from flask_cors import CORS
from flask import Flask, request, redirect, render_template

from models.translate import translate, get_token, deepseek_v3_translate

app = Flask(__name__)
CORS(app, resources=r'/*')  # 这将为所有路由启用 CORS


@app.route('/index', methods=['GET'])
def index():
    return redirect("/")


@app.route('/', methods=['GET', 'POST'])
def main():  # put application's code here
    translation = {}
    if request.method == "POST":
        text = request.form.get("text")
        mode = request.form.get("mode")
        language = ['auto', 'fra', 'de', 'jp', 'kor', 'pt', 'spa', 'vie', 'id', 'it', 'th', 'tr', 'ru', 'cht', ]
        # language = ['auto', 'fra','de']
        if mode == '0':
            # 百度翻译
            token = get_token("./static/token.txt")
            if not text:
                translation["info"] = "请输入要翻译的文本"
            if text and language and token:
                translation = translate(text, language, token)
        else:
            # deepseek_v3翻译
            translation = deepseek_v3_translate(text, language)
    return render_template('index.html', translation=translation)


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8080, debug=True)
